/*
 * StartFrame.java
 *
 * Created on 2008年5月25日, 下午9:56
 */

package gui;
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.io.*;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
import java.text.*;
import java.net.*;
import abstractmodel.*;
import javax.swing.JPanel;

/**
 *
 * @author  yux
 */
public class MainFrame extends javax.swing.JFrame {    
    private DefaultTreeModel treeModel;
    private DefaultMutableTreeNode[] book;
    private ArrayList<IBook> books;
    private IUser ur;
    private static int initone = 0;
    /**
     * String status represent which panel are using,are one word of these:
     * user(initial state),web,option,and types of books or accounts.
     */
    public static String status = "user";
    public String[] state = {"user", "book", "bank_account","credit_account","money_account", "web"};
    
    private Object popMenuObject;
    public Object currentObject;
    /** Creates new form StartFrame */
    public MainFrame() {
        initComponents();
        String[] allname = Main.mf.getAllUserName();
        jComboBoxName.setModel(new javax.swing.DefaultComboBoxModel(allname));
        this.fresh.setText("刷新");
        this.statistics.setText("统计信息");
        this.addReminder.setText("添加提醒");
        this.filechooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
        this.importDetail.setText("导入明细");
    }

    public void freshjTreeSystem() {
        jTreeSystem.setModel(this.initjTreeSystem());
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItemOut = new javax.swing.JMenuItem();
        jMenuItemExit = new javax.swing.JMenuItem();
        jMenuHelp = new javax.swing.JMenu();
        jMenuItemAbout = new javax.swing.JMenuItem();
        treePopupMenu = new javax.swing.JPopupMenu();
        add = new javax.swing.JMenuItem();
        delete = new javax.swing.JMenuItem();
        addReminder = new javax.swing.JMenuItem();
        statistics = new javax.swing.JMenuItem();
        importDetail = new javax.swing.JMenuItem();
        fresh = new javax.swing.JMenuItem();
        jddeleteuser = new javax.swing.JDialog(this,"删除用户",true);
        jPasswordField1 = new javax.swing.JPasswordField();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        filechooser = new javax.swing.JFileChooser();
        jPanelMain = new javax.swing.JPanel();
        jPanelBegin = new javax.swing.JPanel();
        jLabelName = new javax.swing.JLabel();
        jLabelPw = new javax.swing.JLabel();
        jComboBoxName = new javax.swing.JComboBox();
        jButtonLogin = new javax.swing.JButton();
        jButtonRegister = new javax.swing.JButton();
        jPasswordField = new javax.swing.JPasswordField();
        jLabelPwwrong = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPanelLogin = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanelAccount = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTreeSystem = new javax.swing.JTree();
        jPanelAccountShow = new javax.swing.JPanel();
        jPanelWeb = new javax.swing.JPanel();
        jButtonSet = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        jMenu1.setBackground(new java.awt.Color(231, 241, 231));
        jMenu1.setText("文件"); // NOI18N

        jMenuItemOut.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemOut.setText("注销"); // NOI18N
        jMenuItemOut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemOutActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemOut);

        jMenuItemExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemExit.setText("退出 Bye~"); // NOI18N
        jMenuItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemExitActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemExit);

        jMenuBar1.add(jMenu1);

        jMenuHelp.setBackground(new java.awt.Color(231, 241, 231));
        jMenuHelp.setText("帮助"); // NOI18N
        jMenuHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuHelpActionPerformed(evt);
            }
        });

        jMenuItemAbout.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemAbout.setText("关于"); // NOI18N
        jMenuItemAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemAboutActionPerformed(evt);
            }
        });
        jMenuHelp.add(jMenuItemAbout);

        jMenuBar1.add(jMenuHelp);

        add.setText("Item");
        add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addActionPerformed(evt);
            }
        });
        treePopupMenu.add(add);

        delete.setText("Item");
        delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteActionPerformed(evt);
            }
        });
        treePopupMenu.add(delete);

        addReminder.setText("Item");
        addReminder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addReminderActionPerformed(evt);
            }
        });
        treePopupMenu.add(addReminder);

        statistics.setText("Item");
        statistics.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                statisticsActionPerformed(evt);
            }
        });
        treePopupMenu.add(statistics);

        importDetail.setText("Item");
        importDetail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importDetailActionPerformed(evt);
            }
        });
        treePopupMenu.add(importDetail);

        fresh.setText("Item");
        fresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                freshActionPerformed(evt);
            }
        });
        treePopupMenu.add(fresh);

        jPasswordField1.setText("jPasswordField1");

        jButton2.setText("确认");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setText("输入密码：");

        jButton3.setText("取消");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jddeleteuserLayout = new javax.swing.GroupLayout(jddeleteuser.getContentPane());
        jddeleteuser.getContentPane().setLayout(jddeleteuserLayout);
        jddeleteuserLayout.setHorizontalGroup(
            jddeleteuserLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jddeleteuserLayout.createSequentialGroup()
                .addGroup(jddeleteuserLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jddeleteuserLayout.createSequentialGroup()
                        .addGap(124, 124, 124)
                        .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jddeleteuserLayout.createSequentialGroup()
                        .addGap(77, 77, 77)
                        .addComponent(jLabel1))
                    .addGroup(jddeleteuserLayout.createSequentialGroup()
                        .addGap(97, 97, 97)
                        .addComponent(jButton2)
                        .addGap(65, 65, 65)
                        .addComponent(jButton3)))
                .addContainerGap(128, Short.MAX_VALUE))
        );
        jddeleteuserLayout.setVerticalGroup(
            jddeleteuserLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jddeleteuserLayout.createSequentialGroup()
                .addGap(65, 65, 65)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41)
                .addGroup(jddeleteuserLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap(67, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("iWallet-SmallPie"); // NOI18N

        jPanelMain.setLayout(new java.awt.CardLayout());

        jLabelName.setFont(new java.awt.Font("宋体", 0, 14));
        jLabelName.setText("用户名"); // NOI18N

        jLabelPw.setFont(new java.awt.Font("宋体", 0, 14));
        jLabelPw.setText("密码"); // NOI18N

        jComboBoxName.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBoxName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxNameActionPerformed(evt);
            }
        });

        jButtonLogin.setText("登录"); // NOI18N
        jButtonLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLoginActionPerformed(evt);
            }
        });

        jButtonRegister.setText("注册"); // NOI18N
        jButtonRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegisterActionPerformed(evt);
            }
        });

        jLabelPwwrong.setFont(new java.awt.Font("宋体", 0, 14));

        jButton1.setText("退出");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelBeginLayout = new javax.swing.GroupLayout(jPanelBegin);
        jPanelBegin.setLayout(jPanelBeginLayout);
        jPanelBeginLayout.setHorizontalGroup(
            jPanelBeginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBeginLayout.createSequentialGroup()
                .addGap(265, 265, 265)
                .addGroup(jPanelBeginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonRegister, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanelBeginLayout.createSequentialGroup()
                        .addGroup(jPanelBeginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelName)
                            .addComponent(jLabelPw))
                        .addGap(27, 27, 27)
                        .addGroup(jPanelBeginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBoxName, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelPwwrong, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanelBeginLayout.createSequentialGroup()
                                .addGroup(jPanelBeginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jPasswordField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(274, Short.MAX_VALUE))
        );
        jPanelBeginLayout.setVerticalGroup(
            jPanelBeginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelBeginLayout.createSequentialGroup()
                .addContainerGap(263, Short.MAX_VALUE)
                .addGroup(jPanelBeginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelName)
                    .addComponent(jComboBoxName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabelPwwrong, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(3, 3, 3)
                .addGroup(jPanelBeginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelPw)
                    .addComponent(jPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonLogin))
                .addGap(35, 35, 35)
                .addGroup(jPanelBeginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonRegister)
                    .addComponent(jButton1))
                .addGap(217, 217, 217))
        );

        jPanelMain.add(jPanelBegin, "card2");

        jTabbedPane1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jTabbedPane1MouseReleased(evt);
            }
        });
        jTabbedPane1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jTabbedPane1StateChanged(evt);
            }
        });

        jPanelAccount.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jPanelAccountMouseReleased(evt);
            }
        });

        jTreeSystem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTreeSystemMouseClicked(evt);
            }
        });
        jTreeSystem.addTreeExpansionListener(new javax.swing.event.TreeExpansionListener() {
            public void treeCollapsed(javax.swing.event.TreeExpansionEvent evt) {
            }
            public void treeExpanded(javax.swing.event.TreeExpansionEvent evt) {
                jTreeSystemTreeExpanded(evt);
            }
        });
        jTreeSystem.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTreeSystemValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jTreeSystem);

        jPanelAccountShow.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jPanelAccountShowMouseReleased(evt);
            }
        });
        jPanelAccountShow.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout jPanelAccountLayout = new javax.swing.GroupLayout(jPanelAccount);
        jPanelAccount.setLayout(jPanelAccountLayout);
        jPanelAccountLayout.setHorizontalGroup(
            jPanelAccountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAccountLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelAccountShow, javax.swing.GroupLayout.DEFAULT_SIZE, 691, Short.MAX_VALUE))
        );
        jPanelAccountLayout.setVerticalGroup(
            jPanelAccountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE)
            .addComponent(jPanelAccountShow, javax.swing.GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("\u8d26\u76ee", jPanelAccount);

        jPanelWeb.setLayout(new java.awt.BorderLayout());
        jTabbedPane1.addTab("\u4eca\u65e5\u8981\u95fb", jPanelWeb);

        jButtonSet.setText("设置"); // NOI18N
        jButtonSet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSetActionPerformed(evt);
            }
        });

        jButton4.setText("向上");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelLoginLayout = new javax.swing.GroupLayout(jPanelLogin);
        jPanelLogin.setLayout(jPanelLoginLayout);
        jPanelLoginLayout.setHorizontalGroup(
            jPanelLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelLoginLayout.createSequentialGroup()
                .addGap(381, 381, 381)
                .addComponent(jButton4)
                .addGap(218, 218, 218)
                .addComponent(jButtonSet)
                .addContainerGap(119, Short.MAX_VALUE))
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 828, Short.MAX_VALUE)
        );
        jPanelLoginLayout.setVerticalGroup(
            jPanelLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelLoginLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSet)
                    .addComponent(jButton4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1))
        );

        jPanelMain.add(jPanelLogin, "card3");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelMain, javax.swing.GroupLayout.DEFAULT_SIZE, 828, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelMain, javax.swing.GroupLayout.DEFAULT_SIZE, 618, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    public IUser getUser() {
        return ur;
    }
    public void setUser(IUser user) {
        ur=user;
    }
    private DefaultTreeModel initjTreeSystem() {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("我的账目");
        if (!getNodes(root)) {
            System.out.println("Initial treemodel fail!");
            return null;
        }
        treeModel = new DefaultTreeModel(root,true);
        currentObject=ur;
        this.jPanelAccountShow.removeAll();
        this.jPanelAccountShow.updateUI();
        return treeModel;
    }
    
    public void changePanel1(JPanel oldp,JPanel newp){
        jPanelAccountShow.remove(oldp);
        jPanelAccountShow.add(newp);
        this.getRootPane().updateUI();
    }
    
    private boolean getNodes (DefaultMutableTreeNode root) {
        books = ur.getBooks();
        int number = books.size();
        if (number <= 0) {
            System.out.println("从用户下找不到任一个账簿！");
            return true;
        }
        book = new DefaultMutableTreeNode[number];
        for (int i = 0; i < number; i++) {
            book[i] = new DefaultMutableTreeNode(((model.BookInfo)(books.get(i).getBookInfo())).getBookName());
            root.add(book[i]);
            int l = books.get(i).getAccounts().size();
            ArrayList<IAccount> accounts = books.get(i).getAccounts();
            for (int j = 0; j < l; j++) {
                if (accounts.get(j).getAccountInfo().getType() == 
                        configure.Global.MONEY_ACCOUNT_INFO_TYPE)
                    book[i].add(new DefaultMutableTreeNode(
                            ((model.MoneyAccountInfo)accounts.get(j).getAccountInfo()).getAccountName()));
                
                else if (accounts.get(j).getAccountInfo().getType() == 
                        configure.Global.BANK_ACCOUNT_INFO_TYPE) 
                    book[i].add(new DefaultMutableTreeNode(
                            ((model.BankAccountInfo)accounts.get(j).getAccountInfo()).getAccountName()));
                
                else if (accounts.get(j).getAccountInfo().getType() == 
                        configure.Global.CREDITCARD_ACCOUNT_INFO_TYPE)
                    book[i].add(new DefaultMutableTreeNode(
                            ((model.CreditCardAccountInfo)accounts.get(j).getAccountInfo()).getAccountName()));           
            }
        }
        return true;
    }
    
    private void jComboBoxNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBoxNameActionPerformed

    private void jButtonLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLoginActionPerformed
        // TODO add your handling code here:
        IModelFactory mf =Main.mf;
        System.out.println(jComboBoxName.getSelectedItem().toString()+"\n"+jPasswordField.getPassword().toString());
        String password=new String(jPasswordField.getPassword());
        System.out.println(jComboBoxName.getSelectedItem().toString()+"\n"+password);
        ur = mf.getUser(jComboBoxName.getSelectedItem().toString(), password);
        if (ur==null) {
            jLabelPwwrong.setText("密码输入错误！");
            return;
        }
        jTreeSystem.setModel(this.initjTreeSystem());
        CardLayout cl = (CardLayout)jPanelMain.getLayout();
        cl.next(jPanelMain);
        this.setJMenuBar(jMenuBar1);    
        this.showSystem("user");
    }//GEN-LAST:event_jButtonLoginActionPerformed

    public void setMenuAndTree(){
        jTreeSystem.setModel(this.initjTreeSystem());
        this.setJMenuBar(jMenuBar1);    
        //this.jPanelAccountShow = new gui.SystemPanel(this, ur);
        this.showSystem("user");
    }
    
    private void jButtonRegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegisterActionPerformed
        // TODO add your handling code here:
        CardLayout cl = (CardLayout)jPanelMain.getLayout();
        jPanelMain.add(new RegisterPanel(this), "card4");
        cl.last(jPanelMain);
    }//GEN-LAST:event_jButtonRegisterActionPerformed

    private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExitActionPerformed
        // TODO add your handling code here:
        System.exit(0);
}//GEN-LAST:event_jMenuItemExitActionPerformed

    private void jMenuItemOutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemOutActionPerformed
        // TODO add your handling code here:
        CardLayout cl = (CardLayout)jPanelMain.getLayout();
        cl.first(jPanelMain);
        this.jLabelPwwrong.setText("");
        this.jPasswordField.setText("");
}//GEN-LAST:event_jMenuItemOutActionPerformed

    public void outPublic () {
        jMenuItemOutActionPerformed(null);
    }
    
    private void jMenuHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuHelpActionPerformed
        // TODO add your handling code here:
        
}//GEN-LAST:event_jMenuHelpActionPerformed

    private void jMenuItemAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemAboutActionPerformed
        // TODO add your handling code here:
        String s = "iWallet的功能包括:\n   储存各类账目信息\n  对账目进行统计分析\n"
                +"  自定义功能设置\n  在线财务信息获取\n";
        javax.swing.JOptionPane.showMessageDialog(this,s,"帮助信息",JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_jMenuItemAboutActionPerformed

    private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jTabbedPane1StateChanged
        // TODO add your handling code here:
        int index = jTabbedPane1.getSelectedIndex();
        System.out.println("index:"+index);
        if (index == 1) 
            jPanelWeb.add(new webPanel(),java.awt.BorderLayout.CENTER);
        else 
            jPanelWeb.removeAll();
    }//GEN-LAST:event_jTabbedPane1StateChanged

    private void jButtonSetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSetActionPerformed
        // TODO add your handling code here:
        javax.swing.JDialog dialog = new javax.swing.JDialog(this);
        dialog.add(new OptionPanel(this,ur));
        dialog.setSize(750, 530);
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
}//GEN-LAST:event_jButtonSetActionPerformed

    private void jTreeSystemValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTreeSystemValueChanged
        // TODO add your handling code here:
        TreePath path = evt.getPath();
        //得到节点的名称
        DefaultMutableTreeNode tn=(DefaultMutableTreeNode) evt.getPath().getLastPathComponent();
        String name = (String)tn.getUserObject();
        System.out.println("name:"+name);

        Object o = searchNodeinTree(name);
        if (o==null) {
            System.out.println("查找节点错误！");
            return;
        }
        setjPanelShow(o);
    }//GEN-LAST:event_jTreeSystemValueChanged

    private void jTreeSystemTreeExpanded(javax.swing.event.TreeExpansionEvent evt) {//GEN-FIRST:event_jTreeSystemTreeExpanded
        // TODO add your handling code here:
    }//GEN-LAST:event_jTreeSystemTreeExpanded

    private void jTabbedPane1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTabbedPane1MouseReleased
        // TODO add your handling code here:
        if(evt.getButton() == MouseEvent.BUTTON3) 
            System.out.println("release tab");
    }//GEN-LAST:event_jTabbedPane1MouseReleased

    private void jPanelAccountMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanelAccountMouseReleased
        // TODO add your handling code here:
        if(evt.getButton() == MouseEvent.BUTTON3) 
            System.out.println("release account");
    }//GEN-LAST:event_jPanelAccountMouseReleased

    private void jPanelAccountShowMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanelAccountShowMouseReleased
        // TODO add your handling code here:
        /*if(evt.getButton() == MouseEvent.BUTTON3) {
            System.out.println("release show");
            ((gui.SystemPanel)this.jPanelAccountShow.getComponent(0))
                    .transfer_listen(evt);
        }*/
    }//GEN-LAST:event_jPanelAccountShowMouseReleased

    private void jTreeSystemMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTreeSystemMouseClicked
        // TODO add your handling code here:
        if(evt.getButton()==java.awt.event.MouseEvent.BUTTON3) {
            TreePath popuMenuPath=this.jTreeSystem.getPathForLocation(evt.getX(),evt.getY());
            if(popuMenuPath==null) {
                showPopmenu(evt,null);
            } else {
                DefaultMutableTreeNode lastCom=(DefaultMutableTreeNode)popuMenuPath.getLastPathComponent();
                String name=(String)lastCom.getUserObject();
                popMenuObject=this.searchNodeinTree(name);
                showPopmenu(evt,popMenuObject);
            }
        }
    }//GEN-LAST:event_jTreeSystemMouseClicked

    private void freshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_freshActionPerformed
        // TODO add your handling code here:
        freshjTreeSystem();
    }//GEN-LAST:event_freshActionPerformed

    private void addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addActionPerformed
        // TODO add your handling code here:
        if (this.add.getText().contains("账簿")) {
            JDialog jd = new JDialog(this, "添加账簿", true);
            jd.add(new AddBookPanel((IUser)popMenuObject));
            jd.setSize(800,600);
            jd.setLocationRelativeTo(this);
            jd.setVisible(true);
        } else if (this.add.getText().contains("帐户")) {
            JDialog jd = new JDialog(this, "添加帐户", true);
            jd.add(new AddAccountPanel((IBook)popMenuObject));
            jd.setSize(800,600);
            jd.setLocationRelativeTo(this);
            jd.setVisible(true);
        } else if (this.add.getText().contains("明细")) {
            JDialog jd = new JDialog(this, "添加明细", true);
            jd.add(new AddDetailPanel((IAccount) popMenuObject));
            jd.setSize(800,600);
            jd.setLocationRelativeTo(this);
            jd.setVisible(true);
        } else {
            System.out.println("see me MainFrame:658");
        }
        this.freshjTreeSystem();
    }//GEN-LAST:event_addActionPerformed

    private void deleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteActionPerformed
        // TODO add your handling code here:
        if(JOptionPane.showConfirmDialog(this, "确认删除？")==JOptionPane.OK_OPTION) {
            if (this.delete.getText().contains("用户")) {
                this.jddeleteuser.setLocationRelativeTo(this);
                this.jddeleteuser.setVisible(true);
                this.jddeleteuser.setSize(800,600);
                return;
            } else if (this.delete.getText().contains("账簿")) {
                ur.deleteBook((IBook)this.popMenuObject);
            } else if (this.delete.getText().contains("帐户")) {
                IBook ownBook=((IAccount)this.popMenuObject).getOwnerBook();
                ownBook.deleteAccount((IAccount)this.popMenuObject);
            } else {
                System.out.println("see me MainFrame:692");
            }
            this.freshjTreeSystem();
        }
    }//GEN-LAST:event_deleteActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        Utilities.cancelDialog(this.jButton3);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        if(ur.checkCode(new String(this.jPasswordField1.getPassword()))) {
            Main.mf.deleteUser(ur);
            Utilities.cancelDialog(this.jButton2);
            CardLayout cl=(CardLayout) this.jPanelMain.getLayout();
            cl.first(this.jPanelMain);
        }else {
            JOptionPane.showMessageDialog(this, "密码错误");
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void addReminderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addReminderActionPerformed
        // TODO add your handling code here:
        if(this.popMenuObject instanceof model.CreditCardAccount) {
            JDialog jd=new JDialog(this,"添加提醒",true);
            jd.setSize(350,430);
            jd.add(new AddCreditcardReminder(ur,(IAccount)this.popMenuObject));
            jd.setLocationRelativeTo(this);
            jd.setVisible(true);
        }else if (this.popMenuObject instanceof IAccount){
            JDialog jd=new JDialog(this,"添加提醒",true);
            jd.setSize(350,430);
            jd.add(new AddRemainReminder(ur,(IAccount)this.popMenuObject));
            jd.setLocationRelativeTo(this);
            jd.setVisible(true);
        }
    }//GEN-LAST:event_addReminderActionPerformed

    private void importDetailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importDetailActionPerformed
        // TODO add your handling code here:
        this.filechooser.setDialogTitle("打开导入文件");
        int returnValue=this.filechooser.showOpenDialog(this);
        this.filechooser.setMultiSelectionEnabled(true);
        if(returnValue==JFileChooser.APPROVE_OPTION) {
            File file=this.filechooser.getSelectedFile();
            Utilities.inputDetailFromFile((IAccount)popMenuObject, file.getPath());
            ArrayList a=((IAccount)popMenuObject).getDetailsInfo();
            for(Object o:a) {
                System.out.println(o);
            }
            setjPanelShow(popMenuObject);
         }   
    }//GEN-LAST:event_importDetailActionPerformed

    private void statisticsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_statisticsActionPerformed
        // TODO add your handling code here:
        JDialog jd=new JDialog(this,"统计信息",true);
        jd.add(new StaticsPanel(popMenuObject));
        jd.setSize(800,600);
        jd.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jd.setLocationRelativeTo(null);
        jd.setVisible(true);
    }//GEN-LAST:event_statisticsActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        if(currentObject instanceof IUser) {
            JOptionPane.showMessageDialog(this, "this is the upmost");
        }else {
            for(IBook b:ur.getBooks()) {
                if(b==currentObject) {
                    currentObject=ur;
                    setjPanelShow(currentObject);
                } else {
                    for(IAccount ac:b.getAccounts()) {
                        if(ac==currentObject) {
                            currentObject=ur;
                            setjPanelShow(currentObject);
                        }
                    }
                }
            }
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    public void setjPanelShow(Object o) {
       jPanelAccountShow.removeAll();
       this.currentObject=o;
       System.out.println(o);
        if (o instanceof IUser) {
            jPanelAccountShow.add(new SystemPanel(this,ur));
        }else if (o instanceof IBook) {
            IBook node = (IBook) o;
            jPanelAccountShow.add(new SystemPanel(this, node));
        }else if (o instanceof model.BankAccount) {
            model.BankAccount node = (model.BankAccount)o;
            jPanelAccountShow.add(new MoneyAndDebitDetail(node));
        }else if (o instanceof model.CreditCardAccount) {
            model.CreditCardAccount node = (model.CreditCardAccount)o;
            jPanelAccountShow.add(new CreditDetailPanel(node));
        }else if (o instanceof model.MoneyAccount) {
            model.MoneyAccount node = (model.MoneyAccount)o;
            jPanelAccountShow.add(new MoneyAndDebitDetail(node));
        }else {
            System.out.println("主窗口状态错误！");
            return;
        }
        this.getRootPane().updateUI();
    }

    /**
     * 将name指代的panel显示在jPanelAccountShow中
     * @param name是账簿/帐户的名字
     * @return
     */
    private boolean showSystem (String name) {
        if (name.equalsIgnoreCase("user")) {
            status = "user";
            //jPanelAccountShow.setLayout(new java.awt.BorderLayout());
            jPanelAccountShow.add(new SystemPanel(this,ur),java.awt.BorderLayout.CENTER);
        }else {
            throw new Error(){};
        }     
        return true;
    }
    
    /**
     * object可能是user, book, 或者某个具体的account，
     * 通过节点的名称在User ur中查询得到相应的对象；
     * @param s
     * @return true当User有这个名字对应的object时；
     */
    private Object searchNodeinTree (String s) {
        if (s.equalsIgnoreCase("我的账目")) {
            status = "user";
            return ur;
        }
        int size = ur.getBooks().size();
        for (int i = 0; i < size; i++) {
            IBook node = ur.getBooks().get(i);
            if (ur.getBooks().get(i).getBookInfo().getValue("账簿名称").getString().equals(s)) {
                status = "book";
                return ur.getBooks().get(i);
            }
            int length = node.getAccounts().size();
            for (int j = 0; j < length; j++) {
                if (node.getAccounts().get(j).getAccountInfo().getValue("帐户名称").getString().equals(s)) {
                    int type = node.getAccounts().get(j).getAccountInfo().getType();
                    if (type == configure.Global.BANK_ACCOUNT_INFO_TYPE){
                        status = "bank_account";
                    }else if (type == configure.Global.CREDITCARD_ACCOUNT_INFO_TYPE) {
                        status = "credit_account";
                    }else if (type == configure.Global.MONEY_ACCOUNT_INFO_TYPE) {
                        status = "money_account";
                    }else 
                        return null;
                    return node.getAccounts().get(j);
                }
            }
        }
        return null;
    }
    private void showPopmenu(java.awt.event.MouseEvent evt,Object o) {
        if(o==null) {
            this.add.setText("添加...");
            this.add.setEnabled(false);
            this.delete.setText("删除...");
            this.delete.setEnabled(false);
            this.addReminder.setEnabled(false);
            this.statistics.setEnabled(false);
            this.importDetail.setEnabled(false);
            this.fresh.setEnabled(true);
            this.treePopupMenu.show((Component)evt.getSource(), evt.getX(), evt.getY());
        }else {
            if(o instanceof IUser) {
                this.add.setText("添加子账簿");
                this.addReminder.setEnabled(false);
                this.importDetail.setEnabled(false);
                this.delete.setText("删除此用户");
            }else if(o instanceof IBook) {          
                this.add.setText("添加子帐户");
                this.addReminder.setEnabled(false);
                this.importDetail.setEnabled(false);
                this.delete.setText("删除此账簿");
            }else if(o instanceof IAccount) {
                this.add.setText("添加子明细");
                this.addReminder.setEnabled(true);
                this.importDetail.setEnabled(true);
                this.delete.setText("删除此帐户");
            }
            this.add.setEnabled(true);
            this.delete.setEnabled(true);
            this.fresh.setEnabled(true);
            this.statistics.setEnabled(true);
            this.treePopupMenu.show((Component)evt.getSource(), evt.getX(), evt.getY());
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem add;
    private javax.swing.JMenuItem addReminder;
    private javax.swing.JMenuItem delete;
    private javax.swing.JFileChooser filechooser;
    private javax.swing.JMenuItem fresh;
    private javax.swing.JMenuItem importDetail;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButtonLogin;
    private javax.swing.JButton jButtonRegister;
    private javax.swing.JButton jButtonSet;
    private javax.swing.JComboBox jComboBoxName;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelName;
    private javax.swing.JLabel jLabelPw;
    private javax.swing.JLabel jLabelPwwrong;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu jMenuHelp;
    private javax.swing.JMenuItem jMenuItemAbout;
    private javax.swing.JMenuItem jMenuItemExit;
    private javax.swing.JMenuItem jMenuItemOut;
    private javax.swing.JPanel jPanelAccount;
    private javax.swing.JPanel jPanelAccountShow;
    private javax.swing.JPanel jPanelBegin;
    private javax.swing.JPanel jPanelLogin;
    private javax.swing.JPanel jPanelMain;
    private javax.swing.JPanel jPanelWeb;
    private javax.swing.JPasswordField jPasswordField;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTree jTreeSystem;
    private javax.swing.JDialog jddeleteuser;
    private javax.swing.JMenuItem statistics;
    private javax.swing.JPopupMenu treePopupMenu;
    // End of variables declaration//GEN-END:variables
    
}
